package xyz.zhouzhaodong.transactional.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.transactional.entity.User;
import xyz.zhouzhaodong.transactional.mapper.UserMapper;
import xyz.zhouzhaodong.transactional.service.IUserService;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2021-11-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    TestServiceImpl twoTestService;

    @Override
    public void oneTest() {
        oneTestNoPublic();
    }

    @Resource
    UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    void oneTestNoPublic(){
        // 插入一条数据
        int num = userMapper.insert(new User("小红", "青岛市", 18));
        if (num > 0){
            // 插入后制造一个错误
            System.out.println(1 / 0);
        }
        // 再次插入一条数据
        userMapper.insert(new User("小强", "烟台市", 21));
    }

    @Override
    public void twoTest() {
        twoTestService.twoTestNoPublic();
    }

    @Override
    public void threeTest() {
        twoTestService.threeTestNoPublic();
    }
}
